package ms.shop.controller;

import com.alibaba.fastjson.JSONObject;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.string.StringUtil;
import ms.shop.bean.BannerParamBean;
import ms.shop.bean.BannerQueryParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.entity.Banner;
import ms.shop.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Project Name: ms
 * File Name: BannerController
 * Package Name: ms.shop.controller
 * Date: 2018/5/30  16:27
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 分页查询
     * @param bannerQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<Banner> query(@RequestBody BannerQueryParamBean bannerQueryParamBean) {
        if(StringUtil.isEmpty(bannerQueryParamBean.getPageNo())) {
            bannerQueryParamBean.setPageNo(1);
        }
        if(StringUtil.isEmpty(bannerQueryParamBean.getPageSize())) {
            bannerQueryParamBean.setPageSize(10);
        }
        if(StringUtil.isEmpty(bannerQueryParamBean.getName())) {
            bannerQueryParamBean.setName("");
        }
        if(StringUtil.isEmpty(bannerQueryParamBean.getType())) {
            bannerQueryParamBean.setType(0);
        }
        if(StringUtil.isEmpty(bannerQueryParamBean.getUrl())) {
            bannerQueryParamBean.setUrl("");
        }


        String key = JSONObject.toJSONString(bannerQueryParamBean.getName());
        if("\"\"".equals(key)) {
            key = "";
        }
        Page<Banner> page = bannerService.query(bannerQueryParamBean, key);
        return page;
    }

    /**
     * 新增
     * @param bannerParamBean
     * @return
     */
    @PostMapping("/create")
    public void create(@RequestBody BannerParamBean bannerParamBean) {
        //参数校验
        checkParam(bannerParamBean);

        bannerService.create(bannerParamBean);
    }

    /**
     * 修改
     * @param bannerParamBean
     * @return
     */
    @PostMapping("/update")
    public void update(@RequestBody BannerParamBean bannerParamBean) {
        //参数校验
        checkParam(bannerParamBean);

        bannerService.update(bannerParamBean);
    }


    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") Long id) {
        bannerService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") Long id) {
        bannerService.cancel(id);
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public Banner detail(@PathVariable("id") Long id) {
        return bannerService.detail(id);
    }

    /**
     * 清除缓存
     * @return
     */
    @PostMapping("/clear")
    public void clear() {
        bannerService.clear();
    }

    /**
     * 校验新增和修改参数
     * @param bannerParamBean
     */
    private void checkParam(BannerParamBean bannerParamBean) {
        if(StringUtil.isEmpty(bannerParamBean.getId())) {
            if(StringUtil.isEmpty(bannerParamBean.getImg()) || StringUtil.isEmpty(bannerParamBean.getDetailImg())) {
                throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "图片不能为空");
            }
        }
        if(StringUtil.isEmpty(bannerParamBean.getName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "banner名称不能为空");
        }
        if(StringUtil.isEmpty(bannerParamBean.getType())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "banner类型不能为空");
        }
        if(1 != bannerParamBean.getType()) {
            if(StringUtil.isEmpty(bannerParamBean.getUrl())) {
                throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "banner路径不能为空");
            }
        } else {
            bannerParamBean.setUrl("");
        }
    }

}
